package red.lixiang.controller;

import red.lixiang.model.BaseInfo;
import red.lixiang.model.Product;
import red.lixiang.model.WeChatInfo;
import red.lixiang.service.ProductService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;

import static red.lixiang.base.APPCONST.BACK_URL;
import static red.lixiang.base.APPCONST.WECHAT_INFO;

@Controller
public class ProductController {

    protected   final Logger logger = Logger.getLogger(ProductController.class.getName());



    @Autowired
    private ProductService productService;


    @GetMapping("/product/productDetail/{barCode}")
    public String productDetail(@PathVariable String barCode, Model model,
                                HttpServletRequest request,
                                HttpServletResponse response){

        WeChatInfo weChatInfo = (WeChatInfo) request.getSession().getAttribute(WECHAT_INFO);
        if(weChatInfo==null || weChatInfo.getOpenid()==null){
            //如果为空的话，则跳转获取
            request.getSession().setAttribute(BACK_URL,request.getRequestURI());
            try {
                response.sendRedirect("/wx/getAccessCode");
                return null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        BaseInfo baseInfo = productService.getProductByBarCode(barCode);

        Map<String , Object> map = (Map<String, Object>) baseInfo.getObject();
        if(baseInfo.getError().equals("100")){
            //需要重新跳转商品
            try {
                response.sendRedirect("/product/productDetail/"+baseInfo.getMessage());
                return  null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        BaseInfo imageInfo = productService.getProductImageByProductId(((Product)map.get("product")).getProductId());
        Map<String , Object> imageMap = (Map<String, Object>) imageInfo.getObject();
        model.addAllAttributes(map);
        model.addAllAttributes(imageMap);


        return "product/product";

    }

    @GetMapping("/product/getProductBybarCode/{barCode}")
    @ResponseBody
    public String getProductBybarCode(@PathVariable String barCode){

        BaseInfo product = productService.getProductByBarCode(barCode);

        return JSON.toJSONString(product.getObject());

    }

    /**
     * 分页获取商品
     * @param currentPage 当前的页面数
     * @param numPerPage 每页的商品数
     * @return
     */
    @GetMapping("/product/getProductByPage")
    public String getProductByPage(@RequestParam int currentPage,
                                   @RequestParam int numPerPage,
                                   Model model){

        BaseInfo product = productService.getProductByPage(currentPage, numPerPage);
        model.addAttribute("products",product.getObject());
        return "index_macro";

    }


    @GetMapping("/product/changeAttribute")
    @ResponseBody
    public String changeAttribute(@RequestParam String  productId,
                                  @RequestParam int selectType,
                                  @RequestParam String selectValue){

        BaseInfo baseInfo = productService.changeProductAttribute(productId,selectType,selectValue);

        return JSON.toJSONString(baseInfo, SerializerFeature.DisableCircularReferenceDetect);

    }
    @GetMapping("/product/getProductDesc/{productId}")
    @ResponseBody
    public String getProductDesc(@PathVariable String productId){

        BaseInfo baseInfo = productService.getProductDescByProductId(productId);

        return JSON.toJSONString(baseInfo, SerializerFeature.DisableCircularReferenceDetect);

    }

}
